using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using NAudio.Wave;
using System.Collections.Generic;

namespace CompositionalAudio.IO
{
    public class WaveOutPlayer : IPlayer
    {
        WaveOut wavOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
        BufferedWaveProvider provider;

        public WaveOutPlayer(int device, int sampleRate, int channels)
        {
            provider = new BufferedWaveProvider(new WaveFormat(sampleRate, channels));
            provider.DiscardOnBufferOverflow = true;
            wavOut.Init(provider);
        }

        public void SupplyData(byte[] dataIn)
        {
            provider.AddSamples(dataIn, 0, dataIn.Length);
        }

        public void Play()
        {
            wavOut.Play();
        }

        public void Dispose()
        {
            wavOut.Stop();
            wavOut.Dispose();
        }

        public void OnCompleted()
        {
            throw new NotImplementedException();
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void OnNext(byte[] value)
        {
            SupplyData(value);
        }
    }
}
